﻿$(document).ready(function () {
    var roledata = new SmDataTable();
    roledata.Init("select role_id,role_NAME,role_des from base_role");
    $('#rolegrid').datagrid({
        title: '角色管理', //列表的标题       
        iconCls: 'icon-ok',
        width: 'auto',
        height: 'auto',
        nowrap: false,
        striped: true,
        collapsible: true,
        singleSelect: true,
        sortName: 'ROLE_ID',
        sortOrder: 'asc',
        idField: 'ROLE_ID',
        rownumbers: true,
        columns: [[
            { field: 'ROLE_ID', title: '标识码', hidden: true },
            { field: 'ROLE_NAME', title: '角色名',width:'150' },
            { field: 'ROLE_DES', title: '角色描述', width: '300' }
        ]],
        toolbar:
            [{
                text: '角色权限配置',
                iconCls: 'icon-add',
                handler: function () {
                    var node = $('#rolegrid').datagrid('getSelected');
                    if (node == null) {
                        $.messager.alert('提示', '请选择一个角色进行配置。');
                    } else {
                        //myMenu.Init("role_id", node.role_id);
                        $('#rolemenugrid').treegrid({
                            idField: 'MENU_ID',
                            treeField: 'MENU_NAME',
                            columns: [[
                                { field: 'MENU_ID', title: '标识码', width: 30, hidden: true },
                                { field: 'MENU_NAME', title: '模块', width: 150 },
                                //{ field: 'BZ', title: '模块说明', width: 150 },
                                {
                                    field: 'QX', title: '包含', width: 40,
                                    formatter: function (value, row, index) {
                                        if(value=="1")
                                            return "<input type='checkbox' role_NAME='QX' class='chkQX' checked=true  value='" + row.MENU_ID + "' />";
                                        else
                                            return "<input type='checkbox' role_NAME='QX' class='chkQX' value='" + row.MENU_ID + "' />";
                                    }
                                }
                            ]],
                            toolbar:
                                [{
                                    text: '保存',
                                    iconCls: 'icon-edit',
                                    handler: function () {
                                        var ms="";
                                        $("[role_NAME = QX]:checked").each(function () {
                                            var m = this.value;
                                            ms+=m+","
                                        })
                                        ms += "0";
                                        GetSmJSON(CLDJServiceUrl + "/SmwsFrame/SaveRoleMenus", "roleid=" + node.ROLE_ID + "&menuids="+ms, function (d) {
                                            //if (d != "") {
                                                $.messager.alert('提示', '保存成功!', 'info');
                                            //}
                                        });
                                        //$("#rolemenudlg").dialog("close");
                                    }
                                }, '-', {
                                    text: '取消',
                                    iconCls: 'icon-reload',
                                    handler: function () {
                                        $("#rolemenudlg").dialog("close");
                                    }
                                }]
                        });

                        GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/GetRoleMenuTreeGrid", "roleid=" + node.ROLE_ID , function () {
                        	     if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                    var txt= parseResponseText(xmlhttp.responseText)
						                     if (txt!= "") {
                                $('#rolemenugrid').treegrid('loadData', JSON.parse(txt));
                            }
						                }
						                }
                          
                        });


                        $("#rolemenudlg").dialog("open");
                    }
                }
            }]
    });

    $('#rolegrid').datagrid("loadData", roledata.GetEasyuiGridData());

    
})